package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.ErrorMessageListBean;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_WaterList_Adapter;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Garden_MessageList_Activity extends Activity implements View.OnClickListener {



    private TextView tv_noMessageList;

    private Button btn_goBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.garden_messagelist_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        lrv_messageList = (LRecyclerView) findViewById(R.id.lrv_messageList);
        tv_noMessageList= (TextView) findViewById(R.id.tv_noMessageList);

        init();

        btn_goBack.setOnClickListener(this);

    }

    private sPlantAsyncHttpClient sPlantClient;

    private void init() {

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        garden_messageList_adapter = new Garden_MessageList_Adapter(this);
        messageListlRecyclerViewAdapter = new LRecyclerViewAdapter(garden_messageList_adapter);

        lrv_messageList.setHasFixedSize(true);
        lrv_messageList.setLayoutManager(new GridLayoutManager(this, 1));
        lrv_messageList.setAdapter(messageListlRecyclerViewAdapter);
        lrv_messageList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setVertical(R.dimen.gaiaa_devicelist_dh)
                .setColorResource(R.color.garden_color0)
                .build();
        lrv_messageList.addItemDecoration(divider);

        lrv_messageList.setPullRefreshEnabled(false);
        lrv_messageList.setLoadMoreEnabled(false);

        garden_messageList_adapter.setOnItemClickListener(new Garden_MessageList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                ErrorMessageListBean.ErrorMessage msg=garden_messageList_adapter.getDataList().get(pos);
                Intent intent=new Intent(Garden_MessageList_Activity.this,Garden_Message_Activity.class);
                intent.putExtra("msg",msg);
                startActivity(intent);
            }
        });

        getDatas();
    }

    private LRecyclerView lrv_messageList;
    private LRecyclerViewAdapter messageListlRecyclerViewAdapter;
    private Garden_MessageList_Adapter garden_messageList_adapter;

    private void getDatas() {
        RequestParams params = new RequestParams();
        params.put("userId", Global.user.getUserId());

        sPlantClient.get(sPlantAsyncHttpClient.GETMESSAGELIST, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {
                        Gson gson = new Gson();

                        ErrorMessageListBean errorMessageListBean = gson.fromJson(response.toString(), ErrorMessageListBean.class);

                        if (errorMessageListBean.getErrorMessageList() != null && errorMessageListBean.getErrorMessageList().size() > 0) {
                            garden_messageList_adapter.setDataList(errorMessageListBean.getErrorMessageList());
                        }
                        else
                            tv_noMessageList.setVisibility(View.VISIBLE);

                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_MessageList_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_goBack:
                setResult(0);
                finish();
                break;
        }

    }
}
